package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class ResolventTable {
  private static final Log _log = LogFactory.getLog(ResolventTable.class);
  
  private static ResolventTable _instance;
  
  private static final Map<Integer, Integer> _resolvent = new HashMap<>();
  
  public static ResolventTable get() {
    if (_instance == null)
      _instance = new ResolventTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM resolvent");
      rs = ps.executeQuery();
      while (rs.next()) {
        int itemId = rs.getInt("item_id");
        int crystalCount = rs.getInt("crystal_count");
        _resolvent.put(new Integer(itemId), Integer.valueOf(crystalCount));
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("载入溶解物品设置资料数量: " + _resolvent.size() + "(" + timer.get() + "ms)");
  }
  
  public int getCrystalCount(int itemId) {
    int crystalCount = 0;
    if (_resolvent.containsKey(Integer.valueOf(itemId)))
      crystalCount = ((Integer)_resolvent.get(Integer.valueOf(itemId))).intValue(); 
    return crystalCount;
  }
}
